//
//  ViewController.m
//  策略模式
//
//  Created by YuanJiang on 2019/9/5.
//  Copyright © 2019 llbt. All rights reserved.
//

#import "ViewController.h"
#include "CustomField.h"
#import "EmailValidator.h"
#import "PhoneNumberValidator.h"

#define kWidth  [UIScreen mainScreen].bounds.size.width-200
#define kHeight 40

@interface ViewController ()<UITextFieldDelegate>

@property(nonatomic,strong) CustomField *emailField;

@property(nonatomic,strong) CustomField *phoneField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.emailField = [[CustomField alloc] initWithFrame:CGRectMake(100, 200, kWidth, kHeight)];
    self.emailField.delegate = self;
    self.emailField.placeholder = @"请输入邮箱";
    self.emailField.layer.borderColor = [UIColor redColor].CGColor;
    self.emailField.layer.borderWidth = 2;
    self.emailField.validator = [EmailValidator new];
    [self.view addSubview:self.emailField];
    
    
    self.phoneField = [[CustomField alloc] initWithFrame:CGRectMake(100, 400, kWidth, kHeight)];
    self.phoneField.delegate = self;
    self.phoneField.placeholder = @"请输入手机号";
    self.phoneField.layer.borderColor = [UIColor redColor].CGColor;
    self.phoneField.layer.borderWidth = 2;
    self.phoneField.validator = [PhoneNumberValidator new];
    [self.view addSubview:self.phoneField];
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    CustomField *tf = (CustomField *)textField;
    
    if ([tf validate] == NO) {
        NSLog(@"验证失败，失败的原因是：%@",tf.validator.errorMessage);
    }else{
        NSLog(@"验证成功");
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.emailField resignFirstResponder];
    [self.phoneField resignFirstResponder];
}
/**
 这段代码相当于上面的方法
 - (void)textFieldDidEndEditing:(UITextField *)textField {
 
 
 if(textField == 邮箱textField){
 if (textField.text.length <= 0) {
 NSLog(@"邮箱没有输入");
 }
 else{
 NSLog(@"");
 }
 }
 else  if(textField == 手机textField){
 if (textField.text.length <= 0) {
 NSLog(@"手机号没有输入");
 } else {
 if (textField.text.length == 11 ) {
 NSLog(@"");
 } else {
 NSLog(@"请输入正确的手机号码") ;
 }
 }
 }
 
}
*/



@end
